package where2go.sctu.com.zhiyou.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.ArrayList;
import java.util.List;

import where2go.sctu.com.zhiyou.MyApplication;
import where2go.sctu.com.zhiyou.R;
import where2go.sctu.com.zhiyou.entity.ClassifyLeft;

/**
 * ProjectName: zhiyou
 * Created by 张某人
 * CreatedTime:on 2018/4/25   23:02
 * Description:分类fragment
 */
public class ClassifyFragment extends Fragment implements AdapterView.OnItemClickListener {

    // List<PersionInfo> listinfoInfos = new ArrayList<PersionInfo>();
    private ListView listView;
    private ArrayAdapter arrayAdapter;
    private ClassifyRightFragmentOne myFragment;
    private List<ClassifyLeft.DataBean> dataList = new ArrayList<ClassifyLeft.DataBean>();
    private List<String> datas = new ArrayList<String>();


    private String leftUrl = "http://47.104.80.155/youyiyou/classtype/showSecondtype";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.classify_fragment, container, false);

        findView(view);
        return view;
    }

    private void findView(View view) {
        listView = (ListView) view.findViewById(R.id.listview);
        loaderData(leftUrl);
        replaceFragment(new ClassifyRightFragmentOne());
        listView.setOnItemClickListener(this);

    }

    /**
     * 切换fragment内容
     *
     * @param fragment
     */
    private void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager = getChildFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment_container, fragment);
        transaction.commit();
    }

    /**
     * volley+Gson 解析网络数据
     *
     * @param url
     */
    private void loaderData(String url) {
        StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String json) {  //onPostExecute
                Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
                ClassifyLeft classifyLeft = gson.fromJson(json, ClassifyLeft.class);
                dataList = classifyLeft.getData();
                for (int i = 0; i < dataList.size(); i++) {
                    datas.add(dataList.get(i).getTypeName());
                }

                arrayAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, datas);
                listView.setAdapter(arrayAdapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("TAG", volleyError.toString());
                Toast.makeText(getActivity(), "网络请求失败，请检查网络", Toast.LENGTH_SHORT).show();
            }
//        第三部：把请求对象加入到请求的队列当中
        });
        request.setTag("getClassify");
        MyApplication.getHttpQueues().add(request);
    }

    /**
     * ListView 点击事件
     *
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ClassifyRightFragmentOne classifyRightFragmentOne = new ClassifyRightFragmentOne();
        ClassifyRightFragmentTwo classifyRightFragmentTwo = new ClassifyRightFragmentTwo();
        ClassifyRightFragmentThree classifyRightFragmentThree = new ClassifyRightFragmentThree();
        int typeId = dataList.get(position).getTypeId();
        switch (position) {
            case 0:
                replaceFragment(classifyRightFragmentOne);
                break;
            case 1:
                replaceFragment(classifyRightFragmentTwo);
                break;
            case 2:
                replaceFragment(classifyRightFragmentThree);
            default:
                break;
        }

    }

    @Override
    public void onPause() {
        MyApplication.getHttpQueues().cancelAll("getClassify");
        super.onPause();
    }

    @Override
    public void onDestroy() {
        MyApplication.getHttpQueues().cancelAll("getClassify");
        super.onDestroy();
    }
}
